import 'package:flutter/material.dart';
import 'package:get/get_utils/get_utils.dart';
import 'package:keep_accounts/entity/period.dart';
import 'package:keep_accounts/entity/user.dart';
import 'package:objectbox/objectbox.dart';

@Entity()
class WorkRecord {
  @Id()
  int id = 0;

  final user = ToOne<User>();

  final periods = ToMany<Period>();

  int dateTime = DateTime.now().millisecondsSinceEpoch;
  int createTime = DateTime.now().millisecondsSinceEpoch;
  int modifyTime = DateTime.now().millisecondsSinceEpoch;


  String format(TimeOfDay time) {
    return '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
  }

  String periodStr(DayPeriod dayPeriod, BuildContext context) {
    Period? period = periods.firstWhereOrNull((element) => element.dayPeriod == dayPeriod.name);
    if(period == null || period.startTime == null || period.endTime == null) {
      return "无";
    }
    TimeOfDay start = TimeOfDay.fromDateTime(DateTime.fromMillisecondsSinceEpoch(period.startTime!));
    TimeOfDay end = TimeOfDay.fromDateTime(DateTime.fromMillisecondsSinceEpoch(period.endTime!));

    return "${format(start)} —— ${format(end)}";
  }

  Duration periodsDuration() {
    Duration duration = Duration.zero;
    for (var period in periods) {
      duration += period.duration;
    }
    return duration;
  }
}